﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 画图
{
    /// <summary>
    /// 此类实现的功能：
    /// 1、显示图标与画线
    /// 2、选择线条与删除线条
    /// 独立出此类，是为方便移植到其他工程上使用
    /// </summary>
    public class ShowImage2Windows
    {
        public static bool drawFlag = false;
        /// <summary>
        /// 0 绘制模式
        /// 1 选择模式
        /// 2 删除模式
        /// </summary>
        public static int drawMode = 0;
        /// <summary>
        /// 图标路径
        /// </summary>
        public static string ImagePath = "";


        private static Bitmap MyImage;

        public static void ShowMyImage(PictureBox pictureBox, String fileToDisplay, int xSize, int ySize) {

            // Sets up an image object to be displayed.
            if (MyImage != null) {
                MyImage.Dispose();
            }

            // Stretches the image to fit the pictureBox.
            pictureBox.SizeMode = PictureBoxSizeMode.StretchImage;
            MyImage = new Bitmap(fileToDisplay);
            pictureBox.ClientSize = new Size(xSize, ySize);
            pictureBox.Image = (Image)MyImage;
        }


        // 为了方便,直接在一个函数中完成
        public static void DrawLines(PictureBox pictureBox)
        {
            // 保存绘制点，为了简单，只保存系列个绘制点的坐标，
            // 根据需要可以加入系列个绘制点间的颜色及线型
            //var points = new List<Point>();
            List<LineCoordinates> LinePoints = new List<LineCoordinates>();
            // 界面重绘
            //panel.Paint += (sender, args) =>
            pictureBox.Paint += (sender, args) => {

                //Debug.Print("Paint 重绘于：" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"));

                //先添加图片
                ShowMyImage(pictureBox, "main.bmp", ((PictureBox)sender).Width, ((PictureBox)sender).Height);

                foreach (var line in LinePoints) {
                    if (line.PointNum >= 2)
                    { // 绘制出所有绘制点之间的线条,这里为了方便,直接将颜色/线型写入代码中
                        // 可以根据需要从界面设置中获取参数
                        if (line.Selected)
                        {//选中
                            using (var pen = new Pen(Color.Green, 7)) { //画线
                                args.Graphics.DrawLines(pen, line.Points.ToArray());
                            }
                        } else {//没选中
                            using (var pen = new Pen(Color.Black, 7)) { //画线
                                args.Graphics.DrawLines(pen, line.Points.ToArray());
                            }
                        }
                    }

                    if (line.PointNum == (line.StartIndex + 1))//做一下限制 ，当只有起始点时，才做虚线
                    {// 绘制出最后一个与当前鼠标点之间的线条,这里为了方便,直接将颜色/线型写入代码中
                        // 可以根据需要从界面设置中获取参数
                        using (var pen = new Pen(Color.Red, 5)) {
                            var curPostion = pictureBox.PointToClient(Cursor.Position);
                            pen.DashStyle = DashStyle.DashDot;
                            args.Graphics.DrawLine(pen, line.Points[0], curPostion);
                        }
                    }
                }

            };

            // 鼠标移动时直接要求重绘
            //panel.MouseMove += (sender, args) =>
            pictureBox.MouseMove += (sender, args) => {
                ((PictureBox)sender).Focus();//添加此名，可以在移动时获取焦点
                if (drawFlag == false) return;
                pictureBox.Invalidate();//重绘语句
            };

            // 单击时根据左键或是右键加入绘制点或是清空所有绘制点，然后重绘
            /*panel.MouseClick += (sender, args) =>
            {
                if (args.Button == MouseButtons.Left){
                    if (drawFlag == false) {
                        points.Add(new Point(args.X, args.Y));
                        drawFlag = true;
                    }
                } else if (args.Button == MouseButtons.Right){
                    if (drawFlag == true) {
                        points.Add(new Point(args.X, args.Y));
                        //points.Clear();
                        drawFlag = false;
                    }
                }
                panel.Invalidate();
            };*/


            //panel.MouseDown += (sender, args) => {
            pictureBox.MouseDown += (sender, args) => {

                //Debug.Print("点击坐标 (" + args.X.ToString() + "," + args.Y.ToString() + ")");
                if (args.Button != MouseButtons.Left) return;

                foreach (var line in LinePoints) line.Selected = false;

                if (drawMode == 0) {
                    //开始
                    LinePoints.Add(new LineCoordinates());
                    LinePoints[LinePoints.Count - 1].Points[LinePoints[LinePoints.Count - 1].StartIndex] = new Point(args.X, args.Y);
                    LinePoints[LinePoints.Count - 1].PointNum = 1;
                    drawFlag = true;
                } else if (drawMode == 1 || drawMode == 2) {
                    foreach (var line in LinePoints) {
                        bool ret = line.CalcSelectedPoint(new Point(args.X, args.Y));
                        if (ret == true && drawMode == 2) {
                            ((PictureBox)sender).Invalidate();//发送重绘要求
                            LinePoints.Remove(line);
                            break;
                        } else if (ret == true) {
                            break;
                        }
                    }
                }

                ((PictureBox)sender).Invalidate();
            };

            //panel.MouseUp += (sender, args) => {
            pictureBox.MouseUp += (sender, args) => {

                if (args.Button != MouseButtons.Left) return;
                if (drawMode != 0) return; //不是绘制模式，不需要继续

                //结束
                LinePoints[LinePoints.Count - 1].Points[LinePoints[LinePoints.Count - 1].EndIndex] = new Point(args.X, args.Y);
                LinePoints[LinePoints.Count - 1].PointNum = 2;
                LinePoints[LinePoints.Count - 1].InitKandB(((PictureBox)sender).Height,((PictureBox)sender).Width);
                drawFlag = false;
                ((PictureBox)sender).Invalidate();
            };

            pictureBox.Resize += (sender, args) => {
                foreach (var line in LinePoints) {
                    line.CalcCanvasZoom(((PictureBox)sender).Height, ((PictureBox)sender).Width);
                }
                ((PictureBox)sender).Invalidate();
            };

            pictureBox.KeyDown += (sender, e) => {
                if (e.KeyCode != Keys.Delete) return;
                foreach (var line in LinePoints) {
                    if (!line.Selected) continue;
                    /*DialogResult result = MessageBox.Show("是否删除选中的线段？", "提示：", MessageBoxButtons.YesNo);
                    if (result == DialogResult.No) break;*/
                    LinePoints.Remove(line);
                    break;
                }
            };
        }

    }
}
